package com.peiwangroup.messageservice.service;

import com.peiwangroup.messageservice.model.WeChatMessage;
import com.peiwangroup.messageservice.model.Order;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WeChatMessageParser implements MessageParser {
    private final ObjectMapper objectMapper = new ObjectMapper();
    
    @Override
    public WeChatMessage parse(String rawMessage) {
        try {
            WeChatMessage message = objectMapper.readValue(rawMessage, WeChatMessage.class);
            
            // 如果消息内容包含订单信息，则解析到Order对象
            if (message.getContent() != null && message.getContent().contains("派单")) {
                parseOrderFromContent(message);
            }
            
            return message;
        } catch (Exception e) {
            throw new RuntimeException("Failed to parse WeChat message", e);
        }
    }
    
    private void parseOrderFromContent(WeChatMessage message) {
        Order order = new Order();
        
        // 解析派单信息
        Pattern pattern = Pattern.compile("派单：(\\S+)");
        Matcher matcher = pattern.matcher(message.getContent());
        if (matcher.find()) {
            order.setDispatch(matcher.group(1));
        }
        
        // 解析接待信息
        pattern = Pattern.compile("接待：(\\S+)");
        matcher = pattern.matcher(message.getContent());
        if (matcher.find()) {
            order.setReceptionist(matcher.group(1));
        }
        
        // 解析陪陪信息
        pattern = Pattern.compile("陪陪：(\\S+)");
        matcher = pattern.matcher(message.getContent());
        if (matcher.find()) {
            order.setCompanion(matcher.group(1));
        }
        
        // 解析老板信息
        pattern = Pattern.compile("老板：(\\S+)");
        matcher = pattern.matcher(message.getContent());
        if (matcher.find()) {
            order.setCustomer(matcher.group(1));
        }
        
        // 解析服务类型
        pattern = Pattern.compile("类型：(\\S+)");
        matcher = pattern.matcher(message.getContent());
        if (matcher.find()) {
            order.setServiceType(matcher.group(1));
        }
        
        // 解析时长信息
        pattern = Pattern.compile("时长：(\\d+)");
        matcher = pattern.matcher(message.getContent());
        if (matcher.find()) {
            order.setDuration(Integer.parseInt(matcher.group(1)));
        }
        
        // 解析总价信息
        pattern = Pattern.compile("总价：(\\d+)");
        matcher = pattern.matcher(message.getContent());
        if (matcher.find()) {
            order.setTotalPrice(new BigDecimal(matcher.group(1)));
        }
        
        // 解析团抽信息
        pattern = Pattern.compile("团抽：(\\d+)");
        matcher = pattern.matcher(message.getContent());
        if (matcher.find()) {
            order.setGroupCommission(new BigDecimal(matcher.group(1)));
        }
        
        // 解析派抽信息
        pattern = Pattern.compile("派抽：(\\d+)");
        matcher = pattern.matcher(message.getContent());
        if (matcher.find()) {
            order.setDispatchCommission(new BigDecimal(matcher.group(1)));
        }
        
        // 解析接抽信息
        pattern = Pattern.compile("接抽：(\\d+)");
        matcher = pattern.matcher(message.getContent());
        if (matcher.find()) {
            order.setReceptionCommission(new BigDecimal(matcher.group(1)));
        }
        
        // 解析到手金额
        pattern = Pattern.compile("到手：(\\d+)");
        matcher = pattern.matcher(message.getContent());
        if (matcher.find()) {
            order.setFinalAmount(new BigDecimal(matcher.group(1)));
        }
        
        // 解析邀请人信息
        pattern = Pattern.compile("邀请人：(\\S+)");
        matcher = pattern.matcher(message.getContent());
        if (matcher.find()) {
            order.setInviter(matcher.group(1));
        }
        
        // 解析邀请人抽成
        pattern = Pattern.compile("邀请人抽：(\\d+)");
        matcher = pattern.matcher(message.getContent());
        if (matcher.find()) {
            order.setInviterCommission(new BigDecimal(matcher.group(1)));
        }
        
        // 解析备注信息
        pattern = Pattern.compile("备注：(.*)");
        matcher = pattern.matcher(message.getContent());
        if (matcher.find()) {
            order.setRemark(matcher.group(1));
        }
        
        order.setCreateTime(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
        message.setOrder(order);
    }
}